--[[
思路
1、读取目标地图的lni文件夹
2、解析w3e/doo
3、地形信息部分
4、特效部分
5、可破坏物部分
--]]

local Dynamic_terrain = {}
local mt = {}
setmetatable(Dynamic_terrain, Dynamic_terrain)
Dynamic_terrain.__index = mt


--	参数为具体的格子数，左下角默认为 (1,1)
function Dynamic_terrain.new( minx, miny, maxx, maxy )
	local self = setmetatable({}, Dynamic_terrain)
	self.root_path = INPUT_PATH
	self.minx = minx
	self.miny = miny
	self.maxx = maxx
	self.maxy = maxy

	if Mpq.w2l then
		self:init()
		self:analysis_w3e()
		self:analysis_doo()
		self.enable_write = true
	else
		log.error('动态地形只支持lni文件夹格式地图')
	end

	return self
end

function mt:init()
	local w3e_path = ('%s\\map\\war3map.w3e'):format(self.root_path)
	self.w3e = W3e.new( w3e_path )
	self.w3e:load()

	if not self.minx then
		self.minx = 1
	end
	if not self.miny then
		self.miny = 1
	end
	if not self.maxx then
		self.maxx = self.w3e.width-1
	end
	if not self.maxy then
		self.maxy = self.w3e.height-1
	end

	local doo_path = ('%s\\map\\war3map.doo'):format(self.root_path)
	local doo = Doodads.new( doo_path )
	doo:load()
	self.doo = doo
end

--[=[
	([[call SetTerrainType( %s, %s, '%s', - 1, 1, 1)]]):format(data.x, data.y, data.type)
{
	{
		x = x,
		y = y,
		type = '',
	},
}
--]=]
function mt:analysis_w3e()
	local minx = self.minx
	local miny = self.miny
	local maxx = self.maxx
	local maxy = self.maxy
	local w3e = self.w3e

	local x = w3e.offset[1]
	local y = w3e.offset[2]
	local types = {}
	local h = 0
	local w = 0
	for height = miny, maxy do
		h = h +1
		w = 0
		for width = minx, maxx do
			w = w + 1
			if not types[w] then
				types[w] = {}
			end
			types[w][h] = {
				x = x + (width-1) * 128,
				y = y + (height-1) * 128,
				type = w3e.corners[width][height].textture_type,
			}
		end
	end
	self.w3e_data = types
end

--[=[
{
	{
		type = 'effect',
		model = [[]],
		x = x,
		y = y,
		z = z,
		angle = angle,
		size = size,
	},
	-- native  CreateDestructableZ  takes integer objectid, real x, real y, real z, real face, real scale, integer variation returns destructable
	{
		type = 'doo',
		id = 'LTlt',
		x = x,
		y = y,
		z = z,
		angle = angle,
		size = size,
		variation = variation,
	},
}
--]=]
function mt:analysis_doo()
	local minx = (self.minx-1) * 128 + self.w3e.offset[1]
	local miny = (self.miny-1) * 128 + self.w3e.offset[2]
	local maxx = (self.maxx) * 128 + self.w3e.offset[1]
	local maxy = (self.maxy) * 128 + self.w3e.offset[2]
	self.doo_data = {}
	for _, obj in ipairs(self.doo.normal_doos) do
		local slk = self:get_slk_by_id(obj.id)
		if slk then
			if obj.position[1] >= minx and obj.position[1] <= maxx and obj.position[2] >= miny and obj.position[2] <= maxy then
				if slk.category == 'D' or slk.category == 'P' then
					--	树木 路径阻隔器
					self.doo_data[#self.doo_data+1] = {
						type = 'doo',
						id = obj.id,
						x = obj.position[1],
						y = obj.position[2],
						z = obj.position[3],
						angle = math.floor( math.deg(obj.angle) ),
						size = math.floor(obj.scale[1] * 1000)/1000,
						variation = obj.variation,
					}
	
				elseif slk.category == 'B' then
					--	桥

				else
					local model
					if slk.file:sub(-4):lower() == '.mdx' or slk.file:sub(-4):lower() == '.mdl' then
						model = slk.file
					else
						model = slk.file .. '.mdx'
					end
					self.doo_data[#self.doo_data+1] = {
						type = 'effect',
						model = model,
						x = obj.position[1],
						y = obj.position[2],
						z = obj.position[3],
						angle = math.floor( math.deg(obj.angle) ),
						size = {
							math.floor(obj.scale[1] * 1000)/1000,
							math.floor(obj.scale[2] * 1000)/1000,
							math.floor(obj.scale[3] * 1000)/1000,
						}
					}
				end
			end
		else
			log.error('该id无法找到slk数据：'..obj.id)
		end
	end
end

function mt:get_slk_by_id( id )
	return Mpq.w2l.slk['doodad'][id] or Mpq.w2l.slk['destructable'][id]
end

function mt:write_out()
	if not self.enable_write then
		log.error('无法写出：未分析成功')
		return
	end

	local output_path =  ('%s\\output\\%s_dt.lua'):format(CURRENT_PATH, Utility.get_file_name(self.root_path))
	local file = io.open(output_path, 'w')
	file:write('return{ w3e = {\n')

	for w = 1, #self.w3e_data do
		for h = 1, #self.w3e_data[w] do
			local v = self.w3e_data[w][h]
			file:write( ("{id='%s',x=%.f,y=%.f},\n"):format(v.type, v.x, v.y) )
		end
	end
	file:write('}, doo={\n')
	for _, v in ipairs(self.doo_data) do
		if v.type == 'doo' then
			file:write( ("{type='doo',id='%s',x=%.f,y=%.f,z=%.f,angle=%.f,size=%s,vari=%.f},\n"):format(v.id, v.x, v.y, v.z, v.angle, v.size, v.variation) )
		else
			file:write( ("{type='effect',model=[[%s]],x=%.f,y=%.f,z=%.f,angle=%.f,size={%s,%s,%s}},\n"):format(v.model, v.x, v.y, v.z, v.angle, v.size[1], v.size[2], v.size[3]) )
		end
	end
	file:write('}, }\n')
	file:close()
end


return Dynamic_terrain